package com.borya.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.borya.dao.TencentDAO;
import com.borya.framework.annotation.Component;
import com.borya.framework.core.ServerSqlSession;
import com.borya.framework.util.ObjectUtils;
import com.borya.model.db.LivingPreLog;
import com.borya.model.db.TencentLivingConf;
import com.borya.util.Constant;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component("tencentDAOImpl")
public class TencentDAOImpl extends BaseDAOImpl implements TencentDAO {

    private Logger log = Logger.getLogger(getClass());


    @Override
    public TencentLivingConf getConf(String packageName) {
        final String key = Constant.Tencent.CONF_KEY;
        final String filed = packageName.replaceAll(",|\\.","");
        final String cacheStr = redis_hget(key,filed);
        if(ObjectUtils.nonNullOrEmpty(cacheStr)){
            return JSONObject.parseObject(cacheStr,TencentLivingConf.class);
        }
        try(ServerSqlSession session = getSqlDataSource().session()) {
            StringBuilder sb = new StringBuilder(128);
            sb.append("select package_name,license,appid,appkey,redis_space,face_type,active");
            sb.append(" from tf_tencent_living_info ");
            List<TencentLivingConf> list = session.executeQuery(sb.toString(), rs->{
                List<TencentLivingConf> lists = null;
                if(rs != null){
                    lists = new ArrayList<>();
                    while(rs.next()){
                        TencentLivingConf conf = new TencentLivingConf();
                        conf.setPackageName(rs.getString(1));
                        conf.setLicense(rs.getString(2));
                        conf.setAppid(rs.getString(3));
                        conf.setAppkey(rs.getString(4));
                        conf.setRedisSpace(rs.getString(5));
                        conf.setFaceType(rs.getInt(6));
                        conf.setActive(rs.getInt(7));
                        lists.add(conf);
                    }
                }
                return lists;
            });
            if(ObjectUtils.isNullOrEmpty(list)){
                log.warn("配置信息查询出为空，"+sb.toString());
                return null;
            }
            Map<String,String> hMap = list.parallelStream().collect(
                    Collectors.toMap(x->x.getPackageName().replaceAll(",|\\.",""), x->JSONObject.toJSONString(x)));
            redis_hmset(key,hMap);
            redis_expire(key,(int) TimeUnit.DAYS.toSeconds(1));
            final String cacheStr1 = redis_hget(key,filed);
            if(ObjectUtils.isNullOrEmpty(cacheStr1)){
                log.warn(String.format("%s无相关腾讯云配置",packageName));
                return null;
            }
            return JSONObject.parseObject(cacheStr1,TencentLivingConf.class);
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public boolean addLivingPreLog(LivingPreLog livingPreLog) {
        StringBuilder sb = new StringBuilder(128);
        sb.append("insert into tb_tencent_living_pre_log ")
                .append("(sys_order_id,check_orderId,face_id)  value (?,?,?)");
        return executeUpdate(sb.toString(),livingPreLog.getSysOrderId(),livingPreLog.getCheckOrderId(),
                livingPreLog.getFaceId());
    }
}
